//-------------------------------------------------------------------
//	Copyright (c) 2010-2012 Zhirnov Andrey
//	This file is part of the "UXGEN" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#ifndef _UX_SHADER_OBJECT_H
#define _UX_SHADER_OBJECT_H

#include "../CResource.h"
#include "../Systems/Render/AllRenderUtils.h"
#include "CShaderProgram.h"

namespace Engine
{
namespace Resources
{

	//
	// Shader Object
	//

	class _EXPORT_ CShader : public CResource
	{
		friend class CShaderProgram;

	protected:
		e_shader::type		_eShaderType;
		GLenum				_eBinaryFormat;
		binary_buffer_t		_aSource;			// string or binary buffer
		GLuint				_uIdent;
		bool				_bIsBinary;
		
	protected:
		CShader(const char *pFileName, e_shader_flags::type eFlags);
		virtual ~CShader();

		virtual bool _Replace(CResource *pRes);
		virtual bool _Load(IFilePtr pFile, uint uSize);
		virtual bool _Save(IFilePtr pFile);
		virtual bool _Reload(IFilePtr pFile, bool bRecursive);

	public:
		bool Create(const char *pShader, uint uLength = 0, e_shader_flags::type eFlags = e_shader_flags::UNKNOWN);
		bool CreateBinary(const uint8 *pBuffer, uint uLength, GLenum eFormat, e_shader_flags::type eFlags);
		bool CompileShader(GLuint &uShader, const string &sPreprocessor) const;

		const binary_buffer_t &	GetSource()		const	{ return _aSource; }
		e_shader::type			GetShaderType()	const	{ return _eShaderType & e_shader::ALL; }
		GLuint					GetIdent()		const	{ return _uIdent; }

		virtual void Destroy();
		
		virtual bool			 IsValid()		const	{ return ( _eShaderType != 0 && !_aSource.Empty() ); }
		virtual e_resource::type GetType()		const	{ return e_resource::SHADER; }

		static CShaderPtr s_CreateShader(const char *pFileName, e_shader_flags::type eFlags);
	};

//-------------------------------------------------------------------

}	// Resources
}	// Engine

#endif	// _UX_SHADER_OBJECT_H